package com.hnkywl.sts.service.zjgl;

/**
 * Created by lw on 2015/11/9.
 */

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.hnkywl.sts.dao.htgl.HtDao;
import com.hnkywl.sts.dao.zjgl.ZrDao;
import com.hnkywl.sts.entity.htgl.Ht;
import com.hnkywl.sts.entity.zjgl.Zr;
import com.hnkywl.sts.service.sys.YgService;
import net.ruixin.orm.hibernate.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Map;

/**
 * 说明：
 * 包:        com.hnkywl.sts.service.zjgl
 * 类描述:   折让服务类
 * 修改备注: [说明本次修改内容]
 * 版本:        [v1.0]
 */
@Service
public class ZrService {
    private ZrDao zrDao;
    private HtDao htDao;

    /**
     * 查询所有折让信息
     *
     * @param htbm
     * @param pageNo
     * @param rowsSize
     * @return
     */
    @Transactional
    public String findAllZr(String htbm, int pageNo, int rowsSize) {
        Page<Map> page = zrDao.findAllZr(htbm, pageNo, rowsSize);
        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
        String json = gson.toJson(page.getResult());
        return "{\"total\":" + page.getTotalCount() + ",\"rows\":" + json + "}";
    }

    /**
     * 根据id查询一条数据的详细
     *
     * @param zrId
     * @return
     */
    @Transactional
    public Zr findZrDetail(Long zrId) {
        return zrDao.get(zrId);
    }

    /**
     * 添加折让记录
     *
     * @param zr
     */
    @Transactional
    public void addZr(Zr zr) {
        zr.setDateCreated(new Date());
        zr.setCreateUser(YgService.getCurrentYg());
        zr.setCreateOrgan(YgService.getCurrentYg().getZggs());
        zr.setCreateDepartment(YgService.getCurrentYg().getZgjg());
        zr.setLastUpdated(new Date());
        zr.setSyzt(0);
        Ht ht = this.htDao.get(zr.getHt().getId());
        zr.setGsjg(ht.getLj().getXy().get(0));
        zr.setBz(zr.getBz());
        zr.setZrje(zr.getZrje());
        this.zrDao.save(zr);
    }

    /**
     * 修改折让
     *
     * @param zr
     */
    @Transactional
    public void updateZr(Zr zr, Long zrId) {
        Zr zr1 = zrDao.get(zrId);
        zr1.setLastUpdated(new Date());
        zr1.setBz(zr.getBz());
        zr1.setZrje(zr.getZrje());
        zr1.setHt(zr.getHt());
        this.zrDao.save(zr1);
    }

    /**
     * 删除
     *
     * @param zrId
     */
    @Transactional
    public void delZr(Long zrId) {
        Zr zr1 = zrDao.get(zrId);
        zr1.setDeleted(1);
        this.zrDao.save(zr1);
    }

    @Autowired
    public void setZrDao(ZrDao zrDao) {
        this.zrDao = zrDao;
    }

    @Autowired
    public void setHtDao(HtDao htDao) {
        this.htDao = htDao;
    }
}
